﻿using System.Collections.Generic;
using System.Linq;
using System.IO;

namespace Common
{
    public class TTSConditionParser : DataPaser
    {
        public TTSConditionParser(string fileName)
            : base(fileName)
        {
            using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                StreamReader sr = new StreamReader(fs);
                List<string> q = new List<string>();
                for (int i = 0; i < 10; i++)
                {
                    string line = sr.ReadLine();
                    if (!string.IsNullOrEmpty(line))
                    {
                        q.Add(line);
                    }
                }
                PartName = q.Where(t => t.Contains("Part name")).Select(t => t.Split('\t')[1]).First();
                Operator = q.Where(t => t.Contains("Operator")).Select(t => t.Split('\t')[1]).First();
                Ordernumber = q.Where(t => t.Contains("Order number")).Select(t => t.Split('\t')[1]).First();
                var a = q.Where(t => t.Contains("Test Item")).First().Split('\t');
                var b = q.Where(t => t.Contains("Test Conditon")).First().Split('\t');
                var d = q.Where(t => t.Contains("Test Pin")).First().Split('\t');
                var e = q.Where(t => t.Contains("Lower limit")).First().Split('\t');
                var f = q.Where(t => t.Contains("Upper limit")).First().Split('\t');
                var g = q.Where(t => t.Contains("Unit")).First().Split('\t');
                for (int i = 1; i < a.Count() - 2; i++)
                {
                    conditions.Add(new TTSCondition() { ItemName = a[i], Condition = b[i], Pin = d[i], LowerLimit = double.Parse(e[i]), UpperLimit = double.Parse(f[i]), Unit = g[i] });
                }
            }

        }
        private static TTSConditionParser _lsCondition = null;
        public static TTSConditionParser getInstance(string fileName)
        {
            if (_lsCondition == null)
            {
                _lsCondition = new TTSConditionParser(fileName);
            }
            return _lsCondition;
        }
        public string PartName { get; set; }
        public string Operator { get; set; }
        public string Ordernumber { get; set; }
        public List<TTSCondition> conditions = new List<TTSCondition>();
    }

    public class TTSCondition
    {
        public string ItemName { get; set; }
        public string Condition { get; set; }
        public string Pin { get; set; }
        public double UpperLimit { get; set; }
        public double LowerLimit { get; set; }
        public string Unit { get; set; }
    }
}
